package com.cigs.wcs00a.dispatch.service.impl;

import com.cigs.wcs00a.dispatch.constant.QGSConstant;
import com.cigs.wcs00a.dispatch.entity.RGV1Entity;
import com.cigs.wcs00a.dispatch.entity.RGV2Entity;
import com.cigs.wcs00a.dispatch.service.DDJService;
import com.cigs.wcs00a.opcua.selfunion.UAService;
import com.cigs.wcs00a.taskPool.QGSThreadPool;
import com.cigs.wcs00a.taskPool.RGV1Runnable;
import com.cigs.wcs00a.taskPool.RGV2Runnable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/16 16:01
 * @Version 1.0
 */
@Service
public class DDJServiceImpl implements DDJService {

    //如果要 用@Autowired 把 单独一个 类 传入一个new出来线程，是不可行的的，
    //因为@Autowired是多处共用一个，传入单独线程，可能会存在线程安全问题。所以不可以
    @Autowired
    private UAService uaService;


//    @Override
//    public Integer warehousingTask(WarehousingEntity warehousingEntity) {
//
//        WarehousingRunnable<Integer> runnable = new WarehousingRunnable<>(uaService,warehousingEntity);
//
//        Future<?> futureTask;
//        synchronized (QGSConstant.LOCK){
//            futureTask = QGSThreadPool.executor.submit(runnable);
//        }
//        Object taskResult = new Object();
//        try {
//            taskResult = futureTask.get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        Integer taskResults = (Integer) taskResult;
//        return taskResults;
//    }

    @Override
    public Integer rgv1Task(RGV1Entity rgv1Entity) {

        RGV1Runnable<Integer> runnable = new RGV1Runnable<>(uaService,rgv1Entity);

        Future<?> futureTask;
        synchronized (QGSConstant.LOCK){
            futureTask = QGSThreadPool.rgv1SinglePool.submit(runnable);
        }
        Object taskResult = new Object();
        try {
            taskResult = futureTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        Integer taskResults = (Integer) taskResult;
        return taskResults;
    }

    @Override
    public Integer rgv2Task(RGV2Entity rgv2Entity) {

        RGV2Runnable<Integer> runnable = new RGV2Runnable<>(uaService,rgv2Entity);

        Future<?> futureTask;
        synchronized (QGSConstant.LOCK){
            futureTask = QGSThreadPool.rgv2SinglePool.submit(runnable);
        }
        Object taskResult = new Object();
        try {
            taskResult = futureTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        Integer taskResults = (Integer) taskResult;
        return taskResults;
    }


}
